/**
 * 
 */
package com.alstudio.core.message;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.module.xmpp.protocol.xmpp.packact.ALMessage;
import com.alstudio.ui.module.notice.SystemNoticeActivity;
import com.alstudio.ui.module.notice.UserNoticeActivity;
import com.alstudio.utils.activity.ALActivityManager;
import com.alstudio.utils.android.notification.ALNotification;
import com.alstudio.utils.android.notification.ALNotification.ALNotificationType;
import com.alstudio.utils.android.notification.ALNotificationCenterManager;
import com.alstudio.utils.android.notification.ALNotificationIdConfig;
import com.alstudio.utils.log.ALLog;
import com.loovee.imaohu.R;

/**
 * @author alonso lee
 * 
 */
public class ALMessageNotificationManager {

	private static ALMessageNotificationManager instance;

	private ALNotificationCenterManager mNotificationCenterManager;
	private Context mContext;

	/**
	 * 
	 */
	public ALMessageNotificationManager() {
		// TODO Auto-generated constructor stub
		mNotificationCenterManager = ALLocalEnv.getLocalEnv()
				.getNotificationCenterManager();
		mContext = ALLocalEnv.getLocalEnv();
	}

	public static ALMessageNotificationManager getDefaultALMessageNotificationManager() {
		if (instance == null) {
			instance = new ALMessageNotificationManager();
		}
		return instance;
	}

	public void cancelSystemNotification() {
		mNotificationCenterManager
				.cancelNotification(ALNotificationIdConfig.NEW_SYSTEM_MESSAGE_NOTIFICATION_ID);
	}

	public void cancelUserMessageNotification() {
		mNotificationCenterManager
				.cancelNotification(ALNotificationIdConfig.NEW_USER_NOTICE_MESSAGE_NOTIFICATION_ID);
	}

	/**
	 * 显示系统消息
	 * 
	 * @param msg
	 * @param isSystemNotice
	 */
	public void showMessageNotification(ALMessage msg, boolean isSystemNotice) {
		if (msg == null || TextUtils.isEmpty(msg.getBody()))
			return;
		Class<?> cls = UserNoticeActivity.class;
		if (isSystemNotice) {
			cls = SystemNoticeActivity.class;
		}
		if (ALActivityManager.getALActivityManager().isActivityOnTop(
				cls.getSimpleName())) {
			ALLog.d("当前在界面内");
			return;
		}

		ALNotification n = buildNotification(cls);

		n.setTicker(msg.getBody());
		n.setContentTitle(msg.getBody());
		n.setContentText(msg.getBody());

		if (isSystemNotice) {
			mNotificationCenterManager
					.cancelNotification(ALNotificationIdConfig.NEW_SYSTEM_MESSAGE_NOTIFICATION_ID);
			n.setId(ALNotificationIdConfig.NEW_SYSTEM_MESSAGE_NOTIFICATION_ID);
		} else {
			mNotificationCenterManager
					.cancelNotification(ALNotificationIdConfig.NEW_USER_NOTICE_MESSAGE_NOTIFICATION_ID);
			n.setId(ALNotificationIdConfig.NEW_USER_NOTICE_MESSAGE_NOTIFICATION_ID);
		}

		mNotificationCenterManager.showNotification(n);

	}

	private ALNotification buildNotification(Class<?> cls) {
		ALNotification n = new ALNotification(mContext,
				ALNotificationType.AL_NOTIFICATION_TYPE_NORMAL, "");
		n.setAutoCancel(false);
		n.setOngoing(false);
		n.setWhen(System.currentTimeMillis());
		n.setSmallIcon(R.drawable.ic_launcher);

		Intent intent = new Intent(mContext, cls);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_SINGLE_TOP);

		PendingIntent contentIntent = PendingIntent.getActivity(mContext,
				ALNotificationIdConfig.NEW_SYSTEM_MESSAGE_NOTIFICATION_ID,
				intent, PendingIntent.FLAG_UPDATE_CURRENT);

		n.setContentIntent(contentIntent);
		n.setId(ALNotificationIdConfig.NEW_SYSTEM_MESSAGE_NOTIFICATION_ID);

		n.setShowing(true);
		return n;
	}

}
